﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;

public partial class Pacientes : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        Page.GetPostBackEventReference(Page);
        //Page.Master.lblUser.Text = Session["txtUserName"].ToString();

        Label headerLabel = (Label)Master.FindControl("lblUser");
        headerLabel.Text = Session["txtUserName"].ToString();

        #region Is Not postback
        if (!IsPostBack)
        {

            string url = Request.Url.ToString();
            string fil = "";
            string cmp = "";
            string acc = "";
            if (url[url.Length - 1].ToString() == "1")
            {
                for (int a = 0; a < url.Length; a++)
                {
                    #region fil
                    if ((url[a].ToString() == "l") && (url[a + 1].ToString() == "="))
                    {
                        int b = a + 2;
                        while (url[b - 1].ToString() != "%")
                        {
                            fil = fil + url[b].ToString();
                            b++;
                        }
                    }
                    #endregion
                    #region cmp
                    if ((url[a].ToString() == "p") && (url[a + 1].ToString() == "="))
                    {
                        int b = a + 2;
                        while (url[b].ToString() != "&")
                        {
                            cmp = cmp + url[b].ToString();
                            b++;
                        }
                    }
                    #endregion
                    #region acc
                    if ((url[a].ToString() == "c") && (url[a + 1].ToString() == "="))
                    {
                        acc = url[a + 2].ToString();
                    }
                    #endregion
                }


                class_paciente.llenar_GridPacientes(fil, cmp, acc, gridPacientes);

            }
            else
                class_paciente.llenar_GridPacientes(gridPacientes, headerLabel.Text);



        }
        #endregion

        #region Ispostback
        if (IsPostBack)
        {

            if (Request.Params.Get("__EVENTTARGET") == "btn_filtrar")
            {
                if (txtfiltro.Value.ToString() != string.Empty)
                {
                    Response.Redirect("Pacientes.aspx?fil=" + txtfiltro.Value.ToString() + "%&cmp=" + selectcampo.Value.ToString() + "&acc=1");
                }
                else class_paciente.llenar_GridPacientes(gridPacientes);
            }


            if (Request.Params.Get("__EVENTTARGET") == "ModificarPostBack")
            {
                
                    class_paciente.modificarPaciente(Convert.ToInt32(txtID.Value), txtNOMBRE.Value, txtAPELLIDO.Value, txtDOB.Value, selectcampo.Value);

            }

            if (Request.Params.Get("__EVENTTARGET") == "InsertarPostBack")
            {

                class_paciente.insertarPaciente(txtNOMBRE.Value, txtAPELLIDO.Value, txtDOB.Value, selectcampo.Value, headerLabel.Text);
            }


        }
        #endregion




    }
    //protected void btn_signOut_Click(object sender, EventArgs e)
    //{
    //    Session["txtUserName"] = null;
    //    Response.Redirect("login.aspx");
    //}
    protected void chk_todos_pacientes_CheckedChanged(object sender, EventArgs e)
    {
        if (chk_todos_pacientes.Checked == true)
        {
            class_paciente.llenar_GridPacientes(gridPacientes);

        }
        else
        {
            class_paciente.llenar_GridPacientes(gridPacientes, Session["txtUserName"].ToString());
        }



    }

    protected void gridPacientes_SelectedIndexChanged(object sender, EventArgs e)
    {
        //string a = gridPacientes.SelectedRow.Cells[4].Text.ToString();
        DataTable dt = class_paciente.consulta_porID(int.Parse(gridPacientes.SelectedRow.Cells[4].Text.ToString()));
        txtID.Value = dt.Rows[0][0].ToString();
        txtNOMBRE.Value = dt.Rows[0][1].ToString();
        txtAPELLIDO.Value = dt.Rows[0][2].ToString();
        txtDOB.Value = dt.Rows[0][3].ToString();
        selextSEX.Value = dt.Rows[0][4].ToString();
        status.Value = "selected";
        lblAlert.Text = "Selected";
        //Session["id_paciente"] = gridPacientes.SelectedRow.Cells[3].Text;
        



    }
}
